-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfix to Postfix.cpp
130 lines (117 loc) · 3.97 KB
/
Infix to Postfix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;
struct Expression {
string type;
bool valid;
};
Expression *expression(string expression){
Expression *exp = new Expression;
bool strExp = expression.find_first_not_of("(+-*/^) abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") == string::npos;
bool numExp = expression.find_first_not_of("(+-*/^) 0123456789") == string::npos;
if(strExp && !numExp){
exp->type = "alphabetic";
} else if(numExp && !strExp){
exp->type = "numeric";
} else {
exp->type = "!";
}
exp->valid = (strExp || numExp) && (strExp != numExp);
return exp;
}
bool isOperand(char data){
return (data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z');
}
bool isOperator(char data){
return (data == '+' || data == '-' || data == '*' || data == '/' || data == '^');
}
bool isBracket(char data){
return (data == '(' || data == ')');
}
int priority(char data){
if(data == '^'){
return 3;
} else if(data == '*' || data == '/'){
return 2;
} else if(data == '+' || data == '-'){
return 1;
} else {
return 0;
}
}
string postfix(string infix){
stack<char> stack;
string result, number;
char data;
Expression *exp = new Expression;
exp = expression(infix);
bool error = (!exp->valid) ? true : false;
// ----- Start Scanning -----/ //
if(!error){
for(int i = 0; i < infix.length(); i++){
data = infix[i];
if(isOperand(data) && exp->type == "alphabetic"){
result += string(1, data) + " ";
} else if(isdigit(data) && exp->type == "numeric") {
number += data;
if(i == infix.length()-1) result += number + " ";
} else if(isBracket(data)){
if(data == '('){
stack.push(data);
} else if(data == ')'){
while(!stack.empty() && stack.top() != '(') {
result += string(1, stack.top()) + " ";
stack.pop();
if(stack.empty()){
error = true;
break;
}
}
if(!stack.empty() && stack.top() == '(') stack.pop();
}
} else if(isOperator(data) || data == ' '){
if(number.length() > 0) result += number + " "; number = "";
if(isOperator(data)){
if(stack.empty() || (!stack.empty() && priority(data) > priority(stack.top()))){
stack.push(data);
} else if(priority(data) < priority(stack.top())){
while(!stack.empty() && priority(data) <= priority(stack.top())){
result += string(1, stack.top()) + " ";
stack.pop();
}
stack.push(data);
} else if(priority(data) == priority(stack.top())){
if(data != '^'){
while(!stack.empty() && priority(data) == priority(stack.top())){
result += string(1, stack.top()) + " ";
stack.pop();
}
}
stack.push(data);
}
}
}
}
}
if(!error){
while(!stack.empty()){
result += stack.top();
stack.pop();
if(!stack.empty()) result += " ";
}
} else {
result = "!";
}
return result;
}
int main(){
string infix = "K+L-M*N+(O^P)*W/U/V*T+Q";
cout << "Infix: " << infix << endl;
string result = postfix(infix);
if(result == "!"){
cout << "INPUT: Invalid Expression!" << endl;
} else {
cout << "Postfix: " << result << endl;
}
}